<include file="../header" />
<body class="J_scroll_fixed">
<script type="text/javascript">
    $(function() {
        $(document).off('change','#upload').on('change','#upload',function(){
            if($("select[name=import_type]").val() == ""){
                alert("请选择【导入类型】");
                $("select[name=import_type]").focus();
                return false;
            }
            if($("select[name=import_store_id]").val() == 0){
                alert("请选择【导入仓库】");
                $("select[name=import_store_id]").focus();
                return false;
            }
            if($("select[name=import_supplier_id]").val() == 0 && $("select[name=import_type]").val() == "物料"){
                alert("请选择【导入供应商】");
                $("select[name=import_supplier_id]").focus();
                return false;
            }
            //上传文件
            $(".top").hide();
            $(".word").show();
            var url;
            if($("select[name=import_type]").val() == "物料")
                url = "{:U('activity/uploadExcel?className=PurchaseMaterial&importType=0')}";
            if($("select[name=import_type]").val() == "成品矫形器")
                url = "{:U('activity/uploadExcel?className=PurchaseFinishedProduct&importType=0')}";
            $.ajaxFileUpload({
                url:url,//处理图片脚本
                secureuri :false,
                fileElementId :'upload',//file控件id
                dataType : 'json',
                success : function (data, status){
//                    alert(data.message);
//                    alert(data.purchase_no);
                    $(".top").show();
                    $(".word").hide();
                    $("input#upload").val("");
                    if(data.count > 0){
                        window.location.href = "/admin/Purchase/importData?purchase_no=" + data.purchase_no + "&type=" + $("select[name=import_type]").val() + "&store_id=" + $("select[name=import_store_id]").val() + "&supplier_id=" + $("select[name=import_supplier_id]").val();
                    }
                    alert("成功导入 " + data.count + " 条数据；重复数据 " + data.duplicated + " 条；无效数据 " + data.invalid + " 条。");
                },
                error: function(data, status, e){
                    alert(e);
                    $(".top").show();
                    $(".word").hide();
                    $("input#upload").val("");
                }
            })

        });

        $("select[name='import_type']").change(function () {
            if($("select[name='import_type']").val() == "物料"){
                $("select[name='import_supplier_id']").show();
            }else{
                $("select[name='import_supplier_id']").hide();
            }
        });

        $("input[name=checkAll]").click(function() {
            var checkboxs = $("input.checkItem");
            if($("input[name=checkAll]").is(':checked')) {
                for(var i = 0 ;i<checkboxs.length;i++) {
                    checkboxs[i].checked = true;
                }
            }else{
                for(var i = 0 ;i<checkboxs.length;i++) {
                    checkboxs[i].checked = false;
                }
            }
        });

        $("a[name=downloadExcel]").click(function() {
            var checkboxs = $("input.checkItem");
            var id = "";
            for (var i = 0; i < checkboxs.length; i++) {
                if(checkboxs[i].checked == true){
                    if(id != ""){
                        id += ",";
                    }
                    id += checkboxs[i].name;
                }
            }
            if(id == ""){
                alert("未选择单据");
                $("input[name=checkAll]").focus();
                return false;
            }
            var url = "/admin/Purchase/getTable?id=" + id;
            window.location.href = url;
        });
    })
</script>
<style type="text/css">
    .btn,.btn-danger,.btn-info,.btn-success,.btn-warning{
        width: 80px;
        /*height: 18px;*/
        padding: 2px 0px 2px 0px;
        margin: 1px 0px 1px 0px;
    }
    .top {
        height: 25px;
        width: 120px;
        background-color: #1dccaa;
        line-height: 25px;
        cursor: pointer;
        display: inline-block;
        text-align: center;
        color: #fff;
        border-radius: 3px;
    }
</style>
<script language="javascript" src="/Template/js/ImgPreview.js"></script>
<script type="text/javascript">
    function del(id, name) {
        if (!confirm("是否删除客户【" + name + "】")) {
            return false;
        }
        var url = "/admin/Purchase/remove?id=" + id;
        window.location.href = url;
    }
</script>
<form class="well form-search" method="post" style="float:left;width:100%;" action="{:U('Purchase/index')}">
    <div class="search_type cc mb10">
        <div class="mb10">
        <span class="mr20">
            单号：<input type="text" name="ps#purchase_no-like" value="{$purchase_no}" />
            &nbsp;&nbsp;
            收货仓：
            <select name="ps#store_id-eq">
                {:getCommonSelectOptionHtml($store_id, "Store", "store_id", "store_name", null, $user['enterprise_code'], 1)}
            </select>
            &nbsp;&nbsp;
            类型：
            <select name="ps#type-eq" style="width: auto">
                {:getFormDictSelectOption($type,'storageType','all',1)}
            </select>
            &nbsp;&nbsp;
            <button class="btn btn-primary">搜索</button>
            <a class="btn" href="{:U('Purchase/index')}">重置</a>
        </span>
        </div>
    </div>
    <hr class="half-rule">

    <div class="controls">
        导入数据：
        <select name="import_type" style="width: auto">
            <option value="">选择类型</option>
            <option value="物料">物料</option>
            <option value="成品矫形器">成品矫形器</option>
        </select>
        &nbsp;&nbsp;
        <select name="import_store_id" style="width: auto">
            {:getCommonSelectOptionHtml($store_id, "Store", "store_id", "store_name", null, $user['enterprise_code'], 1, "选择仓库")}
        </select>
        &nbsp;&nbsp;
        <select name="import_supplier_id" style="width: auto; display: none">
            {:getCommonSelectOptionHtml($supplier_id, "Supplier", "supplier_id", "supplier_name", null, $user['enterprise_code'], 1, "选择供应商")}
        </select>
        &nbsp;&nbsp;
        <label for="upload" class="top">导入Excel文件</label>
        <span class="word" style="color: #de7d74; display: none">导入中。。。</span>
        <input type="file" multiple id="upload" style="display: none;" name="fileToUpload" accept=".xls,.xlsx,.csv"/>
        <br/>
        模板文件：
        <a style="color:#1cc385; font-size: medium; text-decoration:underline;" href="https://xwk-file.oss-cn-qingdao.aliyuncs.com/Home/物料采购导入模板.xlsx">下载【物料采购】Excel模板</a>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a style="color:#45a1de; font-size: medium; text-decoration:underline;" href="https://xwk-file.oss-cn-qingdao.aliyuncs.com/Home/成品采购导入模板.xlsx">下载【成品矫形器采购】Excel模板</a>
        <br/><br/>
        <a name="downloadExcel" style="color: #ffa853; cursor: pointer;">下载选中报表</a>
    </div>
    <hr class="half-rule">
</form>
<div class="wrap J_check_wrap">
    <ul class="nav nav-tabs">

        <li class="active"><a href="{:U('Purchase/index')}">采购列表</a></li>
        <li><a style="color:#1cc385; font-size: medium;" href="{:U('Purchase/purchaseMaterial')}">新增物料采购</a></li>
        <li><a style="color:#45a1de; font-size: medium;" href="{:U('Purchase/purchaseFinishedProduct')}">新增成品矫形器采购</a></li>
    </ul>
    <table class="table table-hover table-bordered">
        <thead>
        <tr>
            <th style="min-width: 10px; width: 10px"><input type="checkbox" name="checkAll"></th>
            <th style="min-width: 160px; width: 160px">单号</th>
            <th style="min-width: 60px; width: 60px">类型</th>
            <th style="min-width: 100px; width: 150px">收货仓</th>
            <th style="min-width: 100px; width: 150px">供货商</th>
            <th style="min-width: 40px; width: 80px">采购人</th>
            <th style="min-width: 40px; width: 80px">审核人</th>
            <th style="min-width: 50px; width: 100px">申请入库日期</th>
            <th style="">备注</th>
            <th style="min-width: 90px; width: 220px">当前状态</th>
            <th style="min-width: 165px; width: 165px">操作</th>
        </tr>
        </thead>
        <tbody>
        <foreach name="list" item="vo">
            <tr>
                <php>
                    $checkName = $vo["purchase_id"];
                </php>
                <td><input type="checkbox" class="checkItem" name="{$checkName}"></td>
                <td>{$vo.purchase_no}</td>
                <if condition = "$vo['type'] eq '物料'">
                    <td style="color: #1cc385;">{$vo.type}</td>
                </if>
                <if condition = "$vo['type'] eq '成品矫形器'">
                    <td style="color: #45a1de;">{$vo.type}</td>
                </if>
                <td>{$vo.store_name}</td>
                <td>{$vo.supplier_name}</td>
                <td>{$vo.purchaser}</td>
                <td>{$vo.auditor}</td>
                <td>{$vo.purchase_date}</td>
                <td style="white-space: pre-wrap">{$vo.remark}</td>
                <if condition = "$vo['status'] eq '-2'">
                    <td style="color: #df6b5f">[已反审，待入库] {$vo.update_time}</td>
                </if>
                <if condition = "$vo['status'] eq '-1'">
                    <td style="color: #df6b5f">[待审核] {$vo.update_time}</td>
                </if>
                <if condition = "$vo['status'] eq '1'">
                    <td style="color: #5993df">[已审核] {$vo.update_time}</td>
                </if>
                <td>
                    <if condition = "$vo['type'] eq '物料'">
                        <a class="btn btn-info" href='{:U("Purchase/purchaseMaterial",array("id"=>$vo["purchase_id"]))}'>查看详情</a>
                    </if>
                    <if condition = "$vo['type'] eq '成品矫形器'">
                        <a class="btn btn-info" href='{:U("Purchase/purchaseFinishedProduct",array("id"=>$vo["purchase_id"]))}'>查看详情</a>
                    </if>
                    <php>
                        $btnText = "入库";
                        $btnStatus = -1;
                        $role = explode(',',$user["role"]);
                        if(in_role($role,"8")){
                        if($vo["status"] == -2 || isNullOrEmpty($vo["status"])){
                        $btnText = "入库";
                        $btnStatus = 1;
                        }else{
                        $btnText = "已入库";
                        $btnStatus = -1;
                        }
                        }
                        if(in_role($role,"4")){
                        if($vo["status"] == -1){
                        $btnText = "审核";
                        $btnStatus = 1;
                        }
                        if($vo["status"] == 1){
                        $btnText = "反审核";
                        $btnStatus = 1;
                        }
                        }
                    </php>
                    <if condition="$btnStatus eq 1 && $vo['type'] eq '物料'">
                        <a class="btn btn-danger" href='{:U("Purchase/purchaseMaterial",array("id"=>$vo["purchase_id"]))}'>{$btnText}</a>
                    </if>
                    <if condition="$btnStatus eq 1 && $vo['type'] eq '成品矫形器'">
                        <a class="btn btn-danger" href='{:U("Purchase/purchaseFinishedProduct",array("id"=>$vo["purchase_id"]))}'>{$btnText}</a>
                    </if>

                </td>
            </tr>
        </foreach>
        </tbody>
    </table>
    <div class="pagination">{$page}</div>
</div>
<script src="__ROOT__/statics/js/common.js"></script>
</body>
</html>